package com.nbjtjc.safe.api.sys;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.bean.runnable.*;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.service.danger.DangerInspectService;
import com.nbjtjc.safe.service.danger.DangerInspectTableLabelService;
import com.nbjtjc.safe.service.emg.EmgTypeService;
import com.nbjtjc.safe.service.know.SafeKnowledgeService;
import com.nbjtjc.safe.service.know.SafeLabelService;
import com.nbjtjc.safe.service.sys.SysPostService;
import com.nbjtjc.safe.util.DataInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @auther guqianbin
 * @date 2018/8/6 14:53
 * 初始化一个公司的数据，复制那些基本的隐患类型，应急处置基本数据
 **/
@RestController
@RequestMapping(value = "/init")
@CrossOrigin("*")
public class InitController {

    @Autowired
    private DangerInspectService dangerInspectService;
    @Autowired
    private DangerInspectTableLabelService dangerInspectTableLabelService;
    @Autowired
    private EmgTypeService emgTypeService;
    @Autowired
    private SafeLabelService safeLabelService;
    @Autowired
    private SafeKnowledgeService safeKnowledgeService;
    @Autowired
    private SysPostService sysPostService;
    private DataInitService dataInitService=DataInitService.getInstance();

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @GetMapping("/initCompanyData/{fromCompanyId}/{toCompanyId}")
    public Result initCompanyData(@PathVariable int fromCompanyId
            , @PathVariable int toCompanyId) throws ExecutionException, InterruptedException {
        Result result=new Result();
        SysDept sysDept=new SysDept();
        sysDept.setId(toCompanyId);
        sysDept=sysDeptMapper.selectByPrimaryKey(toCompanyId);
        if(ObjectUtil.isNull(sysDept)||sysDept.getDataInit()){
            result.setFalse("没有该公司,或者该公司已经初始化数据");
            return result;
        }
        InitDangerInspectRunnable dangerInspectRunnable=new InitDangerInspectRunnable(fromCompanyId,
                toCompanyId,dangerInspectService);
        InitDangerLabelRunnable dangerLabelRunnable=new
                InitDangerLabelRunnable(fromCompanyId,toCompanyId,dangerInspectTableLabelService);
        InitEmgRunnable initEmgRunnable=new InitEmgRunnable(emgTypeService,fromCompanyId,toCompanyId);
        InitKnowledgeRunnable initKnowledgeRunnable=new InitKnowledgeRunnable(safeLabelService,
                safeKnowledgeService, fromCompanyId,toCompanyId);
        InitPostRunnable initPostRunnable=new InitPostRunnable(sysPostService
                ,fromCompanyId,toCompanyId);
        FutureTask<Boolean> dangerInspectTask=new FutureTask(dangerInspectRunnable);
        FutureTask<Boolean> emgTask=new FutureTask<>(initEmgRunnable);
        FutureTask<Boolean> dangerLabelTask=new FutureTask<>(dangerLabelRunnable);
        FutureTask<Boolean> knowledgeTask=new FutureTask<>(initKnowledgeRunnable);
        FutureTask<Boolean> initPostTask=new FutureTask<>(initPostRunnable);
        dataInitService.init(dangerInspectTask);
        dataInitService.init(emgTask);
        dataInitService.init(dangerLabelTask);
        dataInitService.init(knowledgeTask);
        dataInitService.init(initPostTask);
        if(!dangerInspectTask.get()){
            result.setFalse();
            return result;
        }
        if(!emgTask.get()){
            result.setFalse();
            return result;
        }
        if(!dangerLabelTask.get()){
            result.setFalse();
            return result;
        }
        if(!knowledgeTask.get()){
            result.setFalse();
            return result;
        }
        if(!initPostTask.get()){
            result.setFalse();
            return result;
        }
        sysDept.setDataInit(true);
        sysDeptMapper.updateByPrimaryKeySelective(sysDept);
        return result;
    }
}
